import rqdatac as rq
from rqdatac import futures
import pandas as pd
# 获取连续主力合约行情K线数据
def acquire_data(name, start_time, end_time, freq, file_path):
    rq.init()
    data = futures.get_dominant_price(name, start_date=start_time, end_date=end_time, frequency=freq, fields=None, adjust_type='pre', adjust_method='prev_close_ratio')
    print(data.shape)
    # df = data.rename(columns={'datetime': 'date'})
    df = data[['high', 'low', 'open', 'close', 'volume', 'open_interest']]
    print(df.head(2))
    print(df.shape)
    df.to_csv(file_path+name+'_'+freq+'.csv')
    df = pd.read_csv(file_path+name+'_'+freq+'.csv')
    df = df.rename(columns={'datetime': 'date'})
    df = df[['date', 'high', 'low', 'open', 'close', 'volume', 'open_interest']]

    df.to_csv(file_path+name+'_'+freq+'.csv', index=None)
    print("succcess")
    return

# if __name__ =='__main__':
#     name= 'RB' #品种：铁矿石：I 原油：SC
#     start_times=20190101 # 开始时间
#     end_time=20240416  # 结束时间
#     freq='5m' # 频率
#     file_path = "D:\data\datashuju"# 本地保存目录
#     acquire_data(name, start_times, end_time, freq, file_path)
